<?php
declare (strict_types = 1);

namespace app\api\controller\Product;

use app\BaseController;
use app\common\model\Product\Cart as CartModel;
use app\common\model\Product\Product as ProductModel;
use app\common\model\User\User as UserModel;
use Exception;
use think\exception\ValidateException;
use app\common\validate\Product\Cart as CartValiDate;

class Cart extends BaseController
{
    
    public function index()
    {
        $userid = $this->request->param('userid');

        $CartList = CartModel::withJoin(['product'])->where(['userid' => $userid])->select();

        if(!$CartList) {
            $this->error('暂无数据');
        }

        $this->success('查询成功',null,$CartList);
    }

    public function add() {
        try {
            $userid = $this->request->param('userid');

            $proid = $this->request->param('proid');

            $product = ProductModel::find($proid);

            $user = UserModel::find($userid);

            if(!$product) {
                throw new Excepion('商品不存在');
            }
            
            if(!$user) {
                throw new Excepion('用户不存在');
            }

            // 查询购物车是否存在该条数据
            $Cart = CartModel::where(['userid' => $userid,'proid' => $proid]) ->find();

            if($Cart) {
                // 更新购物车的某条记录

                // 判断是否有会员价
                $price = $product['vip_price'] ? $product['vip_price'] : $product['price'];

                $data = [
                    'userid' => $userid,
                    'proid' => $proid,
                    'nums' => $Cart['nums'] + 1,
                    'price' => $price,
                    'total' => bcmul((String)($price),(String)($Cart['nums'] + 1)),
                ];

                $result = CartModel::update($data,['id' => $Cart['id']]);

                if($result === FALSE) {
                    throw new Exception("添加失败");
                }
            }else {
                // 新增

                // 判断是否有会员价
                $price = $product['vip_price'] ? $product['vip_price'] : $product['price'];

                $data = [
                    'userid' => $userid,
                    'proid' => $proid,
                    'nums' => 1,
                    'price' => $price,
                    'total' => $price,
                ];

                validate(CartValiDate::class)->check($data);

                $result = CartModel::create($data);

                if($result === FALSE) {
                    throw new Exception("添加失败");
                    
                }
        }
        } catch (\ValidateException $e) {
            $this->error($e->getError());
        }catch (Exception $error) {
            $this->error($error->getMessage());
        }

        $this->success('添加成功');
    }

    public function stepper() {
        $params = $this->request->param();

        $Cart = CartModel::find($params['cartid']);

        if(!$Cart) {
            $this->error('该购物车记录不存在');
        }

        $Cart->total = bcmul((String)$Cart['price'],(String)$params['nums']);

        $Cart->nums = $params['nums'];

        $result = $Cart->save();

        if($result !== FALSE) {
            $this->success('更新商品的数量成功');
        }else {
            $this->error('更新商品的数量失败');
        }
    }

    public function confirm() {
        $ids = $this->request->param('ids');

        $list = CartModel::withJoin(['product'])->whereIn('cart.id',$ids)->select();

        if(!$list) {
            $this->error('商品不存在');
        }

        $this->success('查询成功',null,$list);
    }
}
